package com.newhis.config;

import com.newhis.properties.MinioProperties;
import com.newhis.thirdpart.CustomMinioClient;
import io.minio.MinioClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfig {
    private static final Logger log = LoggerFactory.getLogger(MinioConfig.class);
    @Autowired
    private MinioProperties minioProperties;

    @Value(value = "${minio.enabled}")
    private boolean minioEnabled;
    @Bean
    public CustomMinioClient minioClient() {
        if (!minioEnabled) {
            log.info("Minio enabled:{}", minioEnabled);
            return null;
        }
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioProperties.getIp(), minioProperties.getPort(), false)
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        CustomMinioClient client = new CustomMinioClient(minioClient);
        log.info("Minio连接成功:{}:{}", minioProperties.getIp(), minioProperties.getPort());
        return client;
    }
}
